package com.government.web.controller.wxpay;


import com.government.common.annotation.RepeatSubmit;
import com.government.common.constant.HttpStatus;
import com.government.common.core.domain.AjaxResult;
import com.government.member.domain.VipOrder;
import com.government.member.service.VipOrderService;
import com.government.wxpay.bo.PaymentBO;
import com.government.wxpay.service.WxPayService;
import com.government.wxpay.vo.NativeResponseVO;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * 微信支付
 */
@RestController
@RequestMapping("/wx/pay")
@Slf4j
public class WxPayController {


    @Autowired
    private WxPayService wxpayService;
    @Autowired
    private VipOrderService vipOrderService;


    /**
     * 小程序预下单
     */
    @RepeatSubmit
    @PostMapping(value = "/prepay")
    public AjaxResult prepay(PaymentBO bo) {
        PrepayWithRequestPaymentResponse result = wxpayService.prepay(bo);
        return AjaxResult.success(result);
    }


    /**
     * 扫码预下单(生成二维码)
     */
    @PostMapping(value = "/nativePrepay")
    public AjaxResult nativePrepay(PaymentBO bo) {
        NativeResponseVO result = wxpayService.nativePrepay(bo);
        return AjaxResult.success(result);
    }

    /**
     * 查询支付状态
     */
    @GetMapping(value = "/payStatus")
    public AjaxResult payStatus(Long orderId) {

        VipOrder vipOrder = vipOrderService.selectVipOrderById(orderId);
        Transaction transaction = wxpayService.nativePayStatus(vipOrder.getOutTradeNo());
        boolean payFlag = false;
        if (Transaction.TradeStateEnum.SUCCESS.name().equals(transaction.getTradeState().name())) {
            payFlag = true;
        }
        return AjaxResult.success(payFlag);
    }

    /**
     * 微信native支付回调通知
     */
    @PostMapping(value = "/native/notice", produces = "application/json;charset=UTF-8")
    public ResponseEntity.BodyBuilder nativePrepayNotice(HttpServletRequest request) throws IOException {

        try {
            wxpayService.nativePrepayNotice(request);
        } catch (Exception e) {
            log.error("微信native支付回调处理失败:", e);
            return ResponseEntity.status(HttpStatus.ERROR);
        }

        // 处理成功，返回 200 OK 状态码
        return ResponseEntity.status(HttpStatus.SUCCESS);
    }

    /**
     * 微信jsapi支付回调通知
     */
    @PostMapping(value = "/jsapi/notice")
    public ResponseEntity.BodyBuilder jsapiPrepayNotice(HttpServletRequest request) throws IOException {

        try {
            wxpayService.jsapiPrepayNotice(request);
        } catch (Exception e) {
            log.error("微信jsapi支付回调处理失败:", e);
            return ResponseEntity.status(HttpStatus.ERROR);
        }

        // 处理成功，返回 200 OK 状态码
        return ResponseEntity.status(HttpStatus.SUCCESS);
    }

}
